package com.hook3.pianke.Adapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hook3.pianke.R;
import com.hook3.pianke.domain.ShopInfo;

import org.xutils.x;

import java.util.List;

/**
 * 商品列表
 * Created by HOME on 2015/12/8.
 */
public class ShopAdapter extends BaseAdapter {
    //数据
    private List<ShopInfo> shopList;
    private Activity activity;
    private ViewHolder viewHolder;

    public ShopAdapter(List shopList, Activity activity) {
        this.shopList = shopList;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return shopList.size();
    }

    @Override
    public Object getItem(int position) {
        return shopList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(activity, R.layout.item_shop, null);
            viewHolder.adapter_shop_cover_img = (ImageView) convertView.findViewById(R.id.adapter_shop_cover_img);
            viewHolder.adapter_shop_name_tx = (TextView) convertView.findViewById(R.id.adapter_shop_name_tx);
            viewHolder.adapter_shop_buy_img = (ImageView) convertView.findViewById(R.id.adapter_shop_buy_img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ShopInfo shopInfo = shopList.get(position);
        x.image().bind(viewHolder.adapter_shop_cover_img, shopInfo.getCoverimg());
        viewHolder.adapter_shop_name_tx.setText(shopInfo.getTitle());
        viewHolder.adapter_shop_buy_img.setTag(shopInfo.getBuyurl());
        viewHolder.adapter_shop_buy_img.setOnClickListener(new MyOnClickListener());
        return convertView;
    }

    class ViewHolder {
        public ImageView adapter_shop_cover_img;
        public TextView adapter_shop_name_tx;
        public ImageView adapter_shop_buy_img;
    }

    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            String url = (String) v.getTag();
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(url);
            intent.setData(content_url);
            activity.startActivity(intent);
        }
    }
}
